package com.kerry.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常类
 * @author Administrator
 */
@ControllerAdvice
public class GolbalExceptionHandler {

	@ExceptionHandler(RuntimeException.class)
	// @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
	// @ExceptionHandler //处理所有异常
	@ResponseBody
	public Result runtimeExceptionHandler(RuntimeException e,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		System.out.println(req.getParameter("username"));
		e.printStackTrace();
		System.out.println(e.getMessage());
		return new Result("-1", "系统运行异常");
	}

	@ExceptionHandler(NullPointerException.class)
	@ResponseBody
	public Result exceptionHandler(NullPointerException e) {
		e.printStackTrace();
		System.out.println(e.getMessage());
		return new Result("-1", "空指针异常");
	}

	public class Result {
		public String code;
		public String msg;

		public Result(String code, String msg) {
			this.code = code;
			this.msg = msg;
		}
	}

}
